package com.osstem;

import java.util.ArrayList;

import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.osstem.model.Staff;

/**
 * 클래스설명 : SearchAmount
 * @version : 2011. 11. 17.
 * @author : jw
 * @분류 : 
 * 설명 : 입력내용을 일자별 / 직원별로 조회
 * ChangeSystem / package com.osstem;
 */

public class SearchAmount extends Activity implements OnClickListener{
	private DbManager dbManager;	//dbManager 객체
	private DatePicker datePicker;
	private ProgressDialog dialog;
	private ArrayList<Staff> staffList;
	private Handler handler;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	    setContentView(R.layout.search_menu);

	    dbInit();
	    
	    Button searchButton = (Button)findViewById(R.id.search);
	    searchButton.setOnClickListener(this);
	    
	    datePicker = (DatePicker)findViewById(R.id.datePicker);
	}
	
	@Override
	public void onClick(View v) {
		//조회시.
		if(v.getId() == R.id.search){
			v.post(new Runnable(){
				@Override
				public void run() {
					dialogProgress();
				}
			});
		}
	}
	
	
	/** 
	 * Method   : dialogProgress
	 * 최초작성일  : 2011. 11. 21. 
	 * 작성자 : jw
	 * 변경이력 :  
	 * Method 설명 : 조회 thread 
	 */
	private void dialogProgress(){
		dialog = ProgressDialog.show(this, "", "조회중입니다...", true);
		handler = new Handler(); 
	 	handler.postDelayed(new Runnable() { 
	 		@Override
	 		public void run() {
	 			search();
	 		}
	 	}, 700);        	
    }    	
    
	
	

	public void dbInit(){
    	dbManager = new DbManager(this);
    }

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if(dbManager != null){
			dbManager.closeReadalbeDb();
			dbManager.closeWritableDb();
		}
	}

	
	
	/** 
	 * Method   : search
	 * 최초작성일  : 2011. 11. 21. 
	 * 작성자 : jw
	 * 변경이력 :  
	 * Method 설명 : 쓰레드용 
	 */
	public void search(){
		staffList = (ArrayList<Staff>)dbManager.selectDateAmount( datePicker.getYear() + "" + (datePicker.getMonth()+1) + "" +datePicker.getDayOfMonth() );		//날짜별 잔액 조회
		makeLayout();		
		dialog.dismiss();
	}
	
	private void makeLayout(){
		
		LinearLayout linearLayout = (LinearLayout)findViewById(R.id.linearLayout);
    	linearLayout.removeAllViews();
    	
    	for(Staff staff: staffList){	    		
    		//직원 이름
    		TextView name = new TextView(this);
    		name.setTextSize(20);
    		name.setTextColor(Color.BLACK);
    		name.setGravity(Gravity.CENTER);
    		name.setText("(" + staff.getName() +")");
    		
    		//환급금액
    		TextView amount = new TextView(this);
    		amount.setTextColor(Color.BLACK);
    		amount.setWidth(160);
    		amount.setTextSize(20);
    		amount.setGravity(Gravity.RIGHT);
    		amount.setText( "("  + staff.getAmount() +  ") 원");
    		
    		LinearLayout newlLinearLayout = new LinearLayout(this);
    		
    		newlLinearLayout.addView(name);
    		newlLinearLayout.addView(amount);
    		linearLayout.addView(newlLinearLayout);
    	}//end for
	}
}
